package hashtables;

/**
 * Testing the hashtable
 * @author Konstantin, Jan
 */
public class Hashtables
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
//        testAddingAndRemoving();
//        testExpandingPerformance();
//        testLoadfactorSearch();
        testDistribution();
    }
    
    /**
     * Testing how often there is a list with a number of elements
     */
    private static void testDistribution()
    {
        Hashtable ht = new Hashtable(10, 0.75);

        ht.fillRandom(5000000);
        
        HashtableTestDistribution.printHashtableDistribution(ht);
    }
    
    /**
     * Testing how the loadfactor affects the search comparisons
     */
    private static void testLoadfactorSearch()
    {
        for (double loadFactor = 0.1; loadFactor < 100; loadFactor += 0.1)
        {
            Hashtable ht = new Hashtable(1, loadFactor);
            
            ht.fillInOrder(50000);
            
            int comparisons = 0;
            
            for (int i = 0; i < 50000; i++)
            {
                comparisons += ht.getSearchComparisons(i);
            }
            
            System.out.printf("%2.1f: %f\n", loadFactor, comparisons / 50000.0);
        }
    }
    
    /**
     * Compares an expanding table with a not expanding one
     */
    private static void testExpandingPerformance()
    {
        long elapsed = 0;
        
        for (int i = 0; i < 10; i++)
        {
            Hashtable ht = new Hashtable(1, 0.75);
            
            long start = System.currentTimeMillis();
            
            ht.fillInOrder(5000000);
            
            elapsed += System.currentTimeMillis() - start;
        }
        
        System.out.println(elapsed / 10.0 / 1000.0);
        
        elapsed = 0;
        
        for (int i = 0; i < 10; i++)
        {
            Hashtable ht = new Hashtable(6666667, 0.75);
            
            long start = System.currentTimeMillis();
            
            ht.fillInOrder(5000000);
            
            elapsed += System.currentTimeMillis() - start;
        }
        
        System.out.println(elapsed / 10.0 / 1000.0);
    }
    
    /**
     * Adds 10 elements and removes all the even ones
     */
    private static void testAddingAndRemoving()
    {
        final int hashtableSize = 10;
        
        Hashtable ht = new Hashtable(1, 0.75);
        
        System.out.println(ht.getCapacity());
        System.out.println(ht.getSize());
        
        ht.fillInOrder(hashtableSize);
        
        HashtableTestDistribution.printBucketDistribution(ht);
        
        System.out.println(ht.getCapacity());
        System.out.println(ht.getSize());
        
        for (int i = 0; i < hashtableSize; i += 2)
        {
            ht.remove(i);
        }
        
        HashtableTestDistribution.printBucketDistribution(ht);
        System.out.println(ht.getCapacity());
        System.out.println(ht.getSize());
    }
}